package com.cip.master.action;

import java.util.List;

import com.cip.admin.model.User;
import com.cip.common.util.SecurityUtil;
import com.cip.master.model.Blog;
import com.cip.master.model.Comments;
import com.cip.master.service.BlogService;
import com.cip.webapp.action.BaseAction;

import org.apache.struts2.ServletActionContext;

public class BlogAction extends BaseAction {
	
	private Blog blog;
	private BlogService blogService;
	private List<Blog> blogList;
	private Comments comments;
	private List<Comments> commentsList;
	private String myBlog;
	
	public String saveOrUpdateBlog() throws Exception {
		try {
			User user = SecurityUtil.getThreadLocalUser().get();
			blog.setTenantId(user.getTenantId());
			blog.setUser(user);
			blogService.saveOrUpdateBlog(blog);			
		} catch (Exception e) {
			e.printStackTrace();
		} 
		return SUCCESS;
	}
	
	public String saveOrUpdateComments() throws Exception{
		try{
		User user = SecurityUtil.threadLocalUser.get();
		comments.setTenantId(user.getTenantId());
		comments.setUser(user);
		comments.setBlog(blog);
		blogService.saveOrUpdateComment(comments);
		}catch(Exception e){
			e.printStackTrace();
		}
		return SUCCESS;
	}
	
	public String createBlog() throws Exception{
		return SUCCESS;
	}
	
	public String showMyBlogList() throws Exception {
		User user = SecurityUtil.getThreadLocalUser().get();
		setBlogList(blogService.getMyBlogsById(user.getId()));
		setMyBlog("true");
		return SUCCESS;
	}
	
	public String showOthersBlogList() throws Exception {
		User user = SecurityUtil.getThreadLocalUser().get();
		setBlogList(blogService.getOthersBlogList(user.getId()));
		setMyBlog("false");
		return SUCCESS;
	}
	
	public String showBlogDetailView() throws Exception {
		setBlog(blogService.getBlogDetails(blog.getId()));
		setCommentsList(blogService.getCommentsByBlogId(blog.getId()));
		return SUCCESS;
	}
	
	public String showEditBlog() throws Exception {
		setBlog(blogService.getBlogDetails(blog.getId()));
		return SUCCESS;
	}
	
	public Blog getBlog() {
		return blog;
	}

	public void setBlog(Blog blog) {
		this.blog = blog;
	}

	public BlogService getBlogService() {
		return blogService;
	}

	public void setBlogService(BlogService blogService) {
		this.blogService = blogService;
	}

	public List<Blog> getBlogList() {
		return blogList;
	}

	public void setBlogList(List<Blog> blogList) {
		this.blogList = blogList;
	}

	public List<Comments> getCommentsList() {
		return commentsList;
	}

	public void setCommentsList(List<Comments> commentsList) {
		this.commentsList = commentsList;
	}

	public Comments getComments() {
		return comments;
	}

	public void setComments(Comments comments) {
		this.comments = comments;
	}

	public String getMyBlog() {
		return myBlog;
	}

	public void setMyBlog(String myBlog) {
		this.myBlog = myBlog;
	}	
	
}
